/**
* All Rights Reserved , Copyright (C) 2022 , 青岛鼎信通讯股份有限公司
*
* MpmProjectPerformanceJudgeService
* 复盘绩效评价表
*
* 修改纪录
* 2022-10-26 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2022-10-26
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.topscomm.basic.BasicEntity;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.pojo.MpmProjectAppointmentEntity;
import com.topscomm.mpm.pojo.MpmProjectPerformanceJudgeEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectPerformanceJudgeService;
import com.topscomm.mpm.service.impl.auto.MpmProjectPerformanceJudgeServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectPerformanceJudgeService")
@Scope(value = "singleton")
public class MpmProjectPerformanceJudgeService extends MpmProjectPerformanceJudgeServiceAuto implements IMpmProjectPerformanceJudgeService {
    @Autowired
    private MpmProjectAppointmentService mpmProjectAppointmentService;
    @Autowired
    private MpmProjectTeamService mpmProjectTeamService;
    /**
     * @author: houyuhang
     * @date: 2022-10-26
     * @param paraMap
     * @return
     * @description:新增复盘时生成人员绩效评价记录
     * @modify:
     */
    @Transactional
    public void generatePerformance(Map<String, Object> paraMap) {
        // 先查询是否存在绩效评价记录，如果存在则不重复生成
        String projectcode = ConvertUtil.convertToString(paraMap.get("projectcode"));
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" projectcode = '{0}' ", projectcode);
        List<MpmProjectPerformanceJudgeEntity> lastEntities = this.queryByWhere(sql.toString());
        if (!CollectionUtil.isEmpty(lastEntities)) {
            return;
        }
        // 获取最新一次已审核任命
        sql.clear();
        sql.appendSingle("enabled=1 and state = 9 and projectcode ='{0}' order by CREATEON desc", projectcode);
        List<MpmProjectAppointmentEntity> mpmProjectAppointmentEntities = mpmProjectAppointmentService.queryByWhere(sql.toString());
        if (CollectionUtil.isEmpty(mpmProjectAppointmentEntities)) {
            return;
        }
        long appointId = mpmProjectAppointmentEntities.get(0).getId();
        // 获取任命组长、组员信息
        sql.clear();
        sql.appendSingle(" MPMPROJECTTEAM.enabled = 1 and appointmentid = '{0}' and projectcode = '{1}' and appointmenttype = '{2}' and (projectrole = '{3}' or projectrole = '{4}') and cbouser.enabled = '1' ",
                appointId, projectcode, MpmSystemConst.MPM_TAEM_APPOINTMENTTYPE_APPOINT, MpmSystemConst.MPM_TEAM_PROJECT_ROLE_LEADER, MpmSystemConst.MPM_TEAM_PROJECT_ROLE_WORKER);
        List<Map<String, Object>> resList = mpmProjectTeamService.queryMapByWhere(sql.toString(), "queryPerformanceJudge");
        List<BasicEntity> basicEntities = new ArrayList<>();
        for (Map<String, Object> resMap : resList) {
            MpmProjectPerformanceJudgeEntity mpmProjectPerformanceJudgeEntity = new MpmProjectPerformanceJudgeEntity();
            basicEntities.add(mpmProjectPerformanceJudgeEntity.convertFromMap(resMap));
        }
        this.insertBatch(basicEntities);
    }
}
